package com.nsoft.html5npub.utils;

import java.util.List;

import com.nsoft.html5npub.R;
import com.nsoft.html5npub.objects.PageInfo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

@SuppressWarnings("deprecation")
public class BookPreviewPanel {
	
	private Context context;
	private ViewGroup parent;
	View popup;
	boolean isVisible = false;
	
	public BookPreviewPanel(Context c,ViewGroup _parent){
		this.context=c;
		this.parent=_parent;

		popup=((Activity)context).getLayoutInflater().
			inflate(R.layout.book_preview_view,parent,false);

		/**
		popup.setOnClickListener(new 
				View.OnClickListener(){
				
			public void onClick(View v){
				hide();
			}
		});
		*/
	}

	public View getView(){
		return this.popup;
	}

	public void show() {
		hide();
		parent.addView(popup);
		
		Gallery g=(Gallery)popup.findViewById(
							R.id.bookPreviewGallery);
		
		List<PageInfo[]> vertPageList=viewengine.
								getVerticalPageList();
		g.setAdapter(new ChapterListAdapter(
					this.context,vertPageList,g));
		
		/**
		 * on click event on each item
		 */
		g.setOnItemClickListener(new AdapterView.
						OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0,
						View arg1,int pos,long arg3) {
				// do something
				
				hide();
			}
		});
		
		isVisible=true;
	}

	public void hide(){
		if (isVisible){
			isVisible=false;
			((ViewGroup)popup.getParent()).
					removeView(popup);
		}
	}
	
	private class ChapterListAdapter extends 
							ArrayAdapter<PageInfo[]> {
		private Context context;
		private List<PageInfo[]> vertPageList;
		private Gallery parent;
		
		public ChapterListAdapter(Context c,
					List<PageInfo[]> _vertPageList,
					Gallery _parent) {
			
			super(c,R.layout.book_preview_cell,_vertPageList);
			this.context=c;
			this.vertPageList=_vertPageList;
			this.parent=_parent;
		}
		
		@Override
		public View getView(int position, View convertView, 
							ViewGroup parent) {
			LayoutInflater inflater=(LayoutInflater)
					this.context.getSystemService(
						Context.LAYOUT_INFLATER_SERVICE);
			
			View view=inflater.inflate(R.layout.
							book_preview_cell,parent,false);
			
			SingleGridView grid=(SingleGridView)view.findViewById(
							R.id.gridVerticalPageList);
			grid.setGalleryParent(this.parent);

			/**
			GridView grid=(GridView)view.findViewById(
					R.id.gridVerticalPageList);
			*/
			
			PageInfo[] pageList=vertPageList.get(position);
			grid.setAdapter(new SingleChapterListAdapter(
									this.context,pageList));
			
			return view;
		}
		
	}
							
	private	class SingleChapterListAdapter extends 
						ArrayAdapter<PageInfo>{
		private PageInfo[] pageList;
		
		public SingleChapterListAdapter(Context context, 
							PageInfo[] objects) {
			super(context,R.layout.book_preview_gridcell,
					objects);
			this.pageList=objects;
		}
		
		@Override
		public View getView(int pos,View convertView,
							ViewGroup parent) {
			
			LayoutInflater inflater=(LayoutInflater)
				BookPreviewPanel.this.context.getSystemService(
						Context.LAYOUT_INFLATER_SERVICE);
			
			View view=inflater.inflate(R.layout.
					book_preview_gridcell,parent,false);
			
			ImageView pageView=(ImageView)view.findViewById(
						R.id.imgPagePreview);
			pageView.setOnClickListener(new PageOnClickListener(
							this.pageList[pos]));
			
			new PageImageLoader(pageView,
					this.pageList[pos]).execute();
			
			return view;
		}
	}
	
	/**
	 * handle the click event of each page image
	 */
	private class PageOnClickListener implements 
						View.OnClickListener{
		private PageInfo page;
		
		public PageOnClickListener(PageInfo _page){
			this.page=_page;
		}
		
		@Override
		public void onClick(View v) {
			viewengine.openPage(this.page);
			BookPreviewPanel.this.hide();
		}
		
	}
	
	/**
	 * loads an image on a different thread
	 */
	private class PageImageLoader extends 
	 			AsyncTask<Void,Integer,Void> {
		ImageView imageView;
		PageInfo page;
		Bitmap bmp;

		public PageImageLoader(ImageView _imgView, 
								PageInfo _page) {
			this.imageView=_imgView;
			this.page=_page;
		}

		@Override
		protected Void doInBackground(Void... params) {
			String htmlPath=this.page.getPath();
			int offs=htmlPath.lastIndexOf(Constant.PATH_SPLITTER);
			String smallImagePath=htmlPath.substring(0,offs+1)+
						Constant.CHR_SMALLSIZE_REMARK+
						this.page.getName();
			this.bmp=engine.getBitmapFromFile(smallImagePath);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			this.imageView.setImageBitmap(bmp);
		}
	}
}
